<template>
    <el-container class="home_container">
        <!-- 头部 -->
        <el-header >
            <div>
                <img src="../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button @click="logout">退出</el-button>
        </el-header>
        <!-- 主体 -->
        <el-container>
            <!-- 左侧边栏 -->
            <el-aside :width="flag?'64px' :'200px'">
                <div class="toggle-button" @click="toggle">｜｜｜</div>
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened='true' :collapse = flag :collapse-transition = "false" router :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id + ''" :key = "item.id" v-for="(item)  in menulist">
                        <!-- 一级菜单模板区域 -->
                        <template slot="title">
                            <!-- 字体图标 -->
                            <i :class="iconsobj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <el-menu-item :index="'/'+item.path" :key="item.id" v-for="(item) in item.children" @click = "saveNavStats('/'+item.path)">
                            <template slot="title">
                                <!-- 字体图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span >{{item.authName}}</span>
                            </template>
                        </el-menu-item>   
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右边主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>



export default {
    data:function(){
        //左侧菜单数据
        return{
            menulist:[],
            iconsobj:{
                /* 一级菜单的id 对应icon图标 */
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao',
            },
            //是否折叠
            flag :false,
            //被激活的链接地址
            activePath:'',
        }
         

    },
    //逐渐一经创建就会执行这个生命周期函数
    created(){
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods:{
        logout:function(){
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // getMenuList: async function(){
        async getMenuList(){
            const {data:res} = await this.$http.get('menus')
            if(res.meta.status !==200)return this.$message.error(res.meta.msg)
            this.menulist = res.data
            console.log(res.data);
        },
        //点击按钮切换菜单折叠与展开
        toggle:function(){
    
           this.flag = !this.flag
      
        },
        //保存连接的激活状态
        saveNavStats:function(activePath){
            window.sessionStorage.setItem('activePath',activePath);
            //点击不同链接时候，给cativePath赋值 ，实现高亮效果的动态切换
            this.activePath = activePath
        }
        
    }
}
</script>

<style lang="less" scoped>
    .home_container {
        height: 100%;
    }
    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left:0 ;
        align-items: center;
        color: #fff;
        font-size: 20px;
        div {
            display: flex;
            align-items: center;
            img {
                width: 60px;
                
            }
            span{
                margin-left: 10px;
            }
        }
    }
    .el-aside{
        background-color: #333744;
        .el-menu{
            border-right: none;
        }
        .toggle-button {
            font-size: 10px;
            line-height:24px ;
            text-align: center;
            color: #fff;
            background-color: #4a5064;
            
            cursor: pointer;
        }
    }
    .el-main {
        background-color: #eaedf1;
    }
    
    .iconfont {
        margin-right:10px ;
    }
</style>